import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex);

const store = new Vuex.Store({
    state:{
        count:100,
        goods:[
            {id:100, name:"剃须刀",price:105.2, num:3},
            {id:101, name:"剃须刀",price:2.11, num:5},
            {id:102, name:"剃须刀",price:9.9, num:19},
            {id:103, name:"剃须刀",price:192.67, num:56},
            {id:104, name:"剃须刀",price:20.9, num:37},
            {id:105, name:"剃须刀",price:30.9, num:47},
            {id:106, name:"剃须刀",price:6.9, num:57},
            {id:107, name:"剃须刀",price:50.9, num:67},
        ],
        profile:{
            userId:"abcdef",
            pwd:"!@#$%123",
            name:"王大爷",
        }
    },
    mutations:{
        increment(state){
            state.count ++;
        },
        decrement(state){
            state.count --;
        },
        addCount(state, payLoad){
            state.count += payLoad.count;
        },
        addGood(state, payLoad){
            // state.goods.push(payLoad);
            state.goods.push(payLoad.newGood);
        },
        //同步的回调函数
        updateProfile(state, payLoad){
            state.profile.name = payLoad.newInfo.name;
            state.profile.userId = payLoad.newInfo.userId;
            state.profile.pwd = payLoad.newInfo.pwd;
            state.profile.sex = payLoad.newInfo.sex;
            //代码执行到这里的时候更新完毕
            payLoad.succeed();
        }
    },
    // 个人信息修改完之后,展示到其他子路由里面
    // 等你这边修改完之后,我在去刷新我的页面
    actions:{
        // 执行期上下文 context -> store
        aupdateProfile(context, payLoad){
            //异步操作
            setTimeout(() => {
                // context.commit("updateProfile", payLoad);
                // context.commit("updateProfile", payLoad);
                console.log(payLoad);
                context.commit("updateProfile", payLoad);
            }, 3000);
        }
    },
    getters:{
        vipGoods(state){
            // const goods = state.goods;
            // const arr = [];
            // for(let i = 0; i< goods.length; i++){
            //   if(goods[i].price > 30){
            //     arr.push(goods[i]);
            //   }
            // }
            // return arr;

            //版本2 你们以后都会越来越强
            return state.goods.filter( item => item.price > 100);
        },
        hotGoods(state, getters){
            return getters.vipGoods.filter( item => item.num > 50);
        },
        userGoods(state){

            const goods = state.goods.filter( item => item.price >20 && item.price < 100);
            //将数组转成字符串
            return state.profile.name + JSON.stringify(goods);
        },
        selectGoods(state, getters){
            // return function(price, num){
            //     // 数据类型转换parInt返回整数 parFloat小数
            //     let p = parseInt(price);
            //     let n = parseInt(num);
            //     //                                   最高价格                最高数量           
            //     console.log(price + typeof(price), num + typeof(num));
            //     return state.goods.filter( item => item.price < p && item.num < n);
            // }
            return (price, num) => state.goods.filter( item => item.price < parseInt(price) && item.num < parseInt(num));
        }
    },
    modules:{
       moduleA:{
           state:{
                name:"老罗",
                tel:"2134234"
           },
           mutations:{
                changeName(state, payLoad){
                    state.name = payLoad;
                }
           },
           actions:{
               // 执行期上下文 context ==> store
                achangeName(context, payLoad){
                    setTimeout(() => {
                        console.log(context);
                        context.commit("changeName", payLoad);
                    }, 2000);
                }
           },
           getters:{
              addName(state){
                  return  "锤子科技" + state.name; 
              },
              updateName(state,getters){
                return  getters.addName + state.tel; 
              },
              finalName(state, getters, rootState){
                return  getters.addName + state.tel + JSON.stringify(rootState.profile); 
              }
           }
       } 
    }
});

export default store;